4
4
.
.
5
5
.
.
8
8
d
d
r
r
a
a
w
w
P
P
o
o
i
i
n
n
t
t
s
s
-
-
P
P
o
o
i
i
n
n
t
t
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorials shows how to use Canvas and drawPoints() to draw Points by setting PointMode.Points.
Points are defined in a List<Offset>.
Syntax
import androidx.compose.foundation.Canvas
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.SolidColor
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f),
Offset(x=700f, y=400f)
),
pointMode = PointMode.Points,
brush = SolidColor(Color.Red),
strokeWidth = 20f
)
}
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we use Canvas to draw Rectangle.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f),
Offset(x=700f, y=400f)
),
pointMode = PointMode.Points,
brush = SolidColor(Color.Red),
strokeWidth = 20f
)
}
}
}
}
Output